package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.PurchaseDTO;
import net.wanho.po.BuyList;
import net.wanho.service.BuyListService;
import net.wanho.service.StoreService;
import net.wanho.vo.PageInfo;
import net.wanho.vo.PurchaseVo;
import net.wanho.vo.R;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("purchase")
public class PurchaseController {
    private final BuyListService buyListService;
    private final StoreService storeService;
    @GetMapping("/purchase-page-list")
    public R purchasePageList(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              PurchaseDTO purchaseDTO){
        PageInfo<PurchaseVo> pageInfo = buyListService.purchasePageList(pageNum, pageSize, purchaseDTO);
        return R.success(pageInfo);
    }
    @GetMapping("/store-list")
    public R storeList(){
        return R.success(storeService.list());
    }

    @PostMapping("/purchase-add")
    public R purchaseAdd(@RequestBody BuyList buyList) {
        buyListService.add(buyList);
        return R.success("采购单添加成功！");
    }

    @PutMapping("/purchase-update")
    public R purchaseUpdate(@RequestBody BuyList buyList) {
        buyListService.updateById(buyList);
        return R.success("采购单修改成功！");
    }

    @PostMapping("/in-warehouse-record-add")
    public R inWarehouseRecordAdd(@RequestBody BuyList buyList) {
        buyListService.inWarehouseRecordAdd(buyList);
        return R.success("入库单添加成功！");
    }

    @DeleteMapping("/purchase-delete/{id}")
    public R purchaseDelete(@PathVariable Integer id) {
        buyListService.removeById(id);
        return R.success("采购单删除成功！");
    }

    @GetMapping("/exportTable")
    public R exportTable(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              PurchaseDTO purchaseDTO){
        PageInfo<PurchaseVo> pageInfo = buyListService.purchasePageList(pageNum, pageSize, purchaseDTO);
        return R.success(pageInfo.getResultList());
    }
}
